// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CameraParameters.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Microsoft.Robotics.Vision.Cameras
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.Text;
    using System.Threading.Tasks;

    using Microsoft.Robotics.Numerics;

    /// <summary>
    /// Class that contains the intrinsics of a camera
    /// </summary>
    [DataContract]
    public class CameraParameters
    {
        /// <summary>
        /// Initializes a new instance of the CameraParameters class
        /// </summary>
        /// <param name="intrinsics">The intrinsics of the matrix</param>
        /// <param name="lensDistortion">The distortion parameters</param>
        /// <param name="halfhorizontalFieldOfViewInRadians">The horizontal field of view in radians</param>
        /// <param name="halfverticalFieldOfViewInRadians">The vertical field of view in radians</param>
        /// <param name="pose">The pose of the camera</param>
        public CameraParameters(
            Matrix3 intrinsics = null,
            LensDistortion lensDistortion = null,
            double halfhorizontalFieldOfViewInRadians = 0,
            double halfverticalFieldOfViewInRadians = 0,
            Pose pose = new Pose())
        {
            this.Intrinsics = intrinsics;
            this.DistortionParameters = lensDistortion;
            this.HalfHorizontalFieldOfViewInRadians = halfhorizontalFieldOfViewInRadians;
            this.HalfVerticalFieldOfViewInRadians = halfverticalFieldOfViewInRadians;
            this.Pose = pose;
        }

        /// <summary>
        /// Gets the intrinsics of the camera
        /// </summary>
        [DataMember]
        public Matrix3 Intrinsics { get; private set; }

        /// <summary>
        /// Gets the distortion parameters (must be of length 5)
        /// </summary>
        [DataMember]
        public LensDistortion DistortionParameters { get; private set; }

        /// <summary>
        /// Gets the Horizontal field of view of the camera.
        /// </summary>
        [DataMember]
        public double HalfHorizontalFieldOfViewInRadians { get; private set; }

        /// <summary>
        /// Gets the Vertical field of view of the camera.
        /// </summary>
        [DataMember]
        public double HalfVerticalFieldOfViewInRadians { get; private set; }

        /// <summary>
        /// Gets or sets the pose of the camera
        /// </summary>
        [DataMember]
        public Pose Pose { get; set; }

        /// <summary>
        /// Creates a intrinsic matrix from parameters
        /// </summary>
        /// <param name="horizontalFocalLength">The horizontal focal length</param>
        /// <param name="verticalFocalLength">The vertical focal length</param>
        /// <param name="horizontalCenter">The horizontal center</param>
        /// <param name="verticalCenter">The vertical center</param>
        /// <param name="skew">The skew</param>
        /// <returns>The intrinsic matrix</returns>
        public static Matrix3 CreateIntrinsics(
            double horizontalFocalLength,
            double verticalFocalLength,
            double horizontalCenter,
            double verticalCenter,
            double skew = 0)
        {
            Matrix3 intrinsics = new Matrix3();
            intrinsics[0, 0] = horizontalFocalLength;
            intrinsics[0, 1] = skew;
            intrinsics[0, 2] = horizontalCenter;

            intrinsics[1, 0] = 0;
            intrinsics[1, 1] = verticalFocalLength;
            intrinsics[1, 2] = verticalCenter;

            intrinsics[2, 0] = 0;
            intrinsics[2, 1] = 0;
            intrinsics[2, 2] = 1;

            return intrinsics;
        }

        /// <summary>
        /// Returns the alignment parameters, if any, to map to a matched image
        /// </summary>
        /// <returns>The alignment parameters</returns>
        public virtual object GetAlignmentParameters()
        {
            return null;
        }
    }
}
